package org.BiteTheDice;
import org.BiteTheDice.R;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.content.DialogInterface;

public class GameEndsMultiPlayer extends Activity implements OnClickListener{

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.end_game_multi);
		TextView view=(TextView) findViewById(R.id.gameScore);
		view.setText("The Winner is:\n"+BiteTheDice.currentWinner+"\n");
		View newButton = findViewById(R.id.newGame);
		newButton.setOnClickListener(this);
		View backButton = findViewById(R.id.backMain);
		backButton.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Music.play(this, R.raw.menumusic);
	//	RunGame.runningWindow=true;
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Music.stop(this);
	//	RunGame.runningWindow=false;
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			Intent j = new Intent(this, BiteTheDice.class);
			startActivity(j);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * Connect Buttons With XML's
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.newGame:
			openNewGameDialog();
			break;
		case R.id.backMain:
			Intent j = new Intent(this, BiteTheDice.class);
			startActivity(j);
			break;
		}
	}

	/**
	 * Opens Select Game type Menu 
	 */
	private void openNewGameDialog() {
		ColorAlertDialog.Builder dialog = new ColorAlertDialog.Builder(this, R.style.YellowAlertDialogTheme);
		dialog.setTitle("Game Type");
		dialog.setPositiveButton("Single Player", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialoginterface,
					int i) {
				startGame(0);	
			}
		});
		dialog.setNegativeButton("Two Players", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialoginterface,
					int i) {
				startGame(1);	
			}
		});	
		dialog.create();
		dialog.show();
	}
	
	/**
	 * Start Game
	 */
	private void startGame(int i) {
		if (i==0){
			//	run single Player game...
			RunGame.setNumPlayer(1);
			Intent j = new Intent(this, RunGame.class);
			startActivity(j);
		}
		else{
			//run multi player game...
			RunGame.setNumPlayer(2);
			Intent j = new Intent(this, RunGame.class);
			startActivity(j);
		}
			
	}
}
